<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>promise</title>
</head>
<body>

</body>
</html>
<script>
    <!--    promise对象用于表示一个异步操作的最终完成(失败),及其结果-->
    var ajax = function (url) {
        return new Promise(function (resolve, reject) {
            // if (Math.random()>0.5){
            // resolve()
            // }else {
            //     reject()
            // }
            setTimeout(() => {
                var data = {
                    data: "我是数据",
                }
                resolve(data)
            }, 1000);
            var xhr = XMLHttpRequest;
            xhr.onload = function (event) {
                resolve()
            }

        })
    }
    console.log("a")
    ajax("/xxx").then((data) => {
        console.log(data)
    }).catch(() => {
        console.log("失败")
    }).finally(() => {
        console.log("加载成功")
    })
    console.log("b")
</script>